﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PrimaryAlgorithm.Intersect
{
    /// <summary>
    /// 1.排序后+双指针
    /// 2.HashMap
    /// </summary>
    class MainProject
    {
        static void Main(string[] args)
        {
            int[] nums1 = new int[] { 1,2,2,1};
            int[] nums2 = new int[] { 2,2 };
            int[]  s=Intersect(nums1, nums2);
            foreach (var item in s )
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();
        }
        public static int[] Intersect(int[] nums1, int[] nums2)
        {
            Dictionary<int, int> dic1 = new Dictionary<int, int>();
            List<int> list = new List<int>();
            foreach (var item in nums1)
            {
                if (dic1.ContainsKey(item))
                {
                    dic1[item]++;
                }
                else
                {
                    dic1.Add(item, 1);
                }
            }
            foreach (var item in nums2)
            {
                if (dic1.ContainsKey(item))
                {
                    list.Add(item);
                    dic1[item]--;
                    if (dic1[item]<=0)
                    {
                        dic1.Remove(item);
                    }
                }
            }
            return list.ToArray();
        }
    }
}
